package com.blockmeta.bbs.overallserviceapplication.search.fragment;

import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.base.fragment.BaseLazyBusinessListFragment;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseCodeResponse;
import com.blockmeta.bbs.businesslibrary.net.pojo.InforPOJO;
import com.blockmeta.bbs.businesslibrary.pojo.FastListPOJO;
import com.blockmeta.bbs.businesslibrary.umeng.ShareRouter;
import com.blockmeta.bbs.businesslibrary.umeng.ShareType;
import com.blockmeta.bbs.businesslibrary.umeng.dialog.BlockMessageShareData;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.blockmeta.bbs.overallserviceapplication.search.adapter.FastListAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;

/**
 * Created by author on 2018/1/3.
 */
@Route(path = RouterPath.FAST_SEARCH_NEWS_FRAGMENT)
public class FastSearchNewsFragment extends BaseLazyBusinessListFragment<FastListPOJO> implements FastListAdapter.OnSharedClickListener{
    private String mLastPost = "";
    private FastListAdapter mListAdapter;
    private boolean isloadmore;
    @Autowired(name = Constant.ZERO)
    public String mKey;
    @Autowired(name = Constant.ONE)
    public String mValue;
    @Autowired(name = Constant.THREE)
    public boolean mIM;

    public static FastSearchNewsFragment newInstance(String key1, String value1) {
        FastSearchNewsFragment fragment = new FastSearchNewsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.ZERO, key1);
        bundle.putString(Constant.ONE, value1);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public BaseQuickAdapter initAdapter() {
        mListAdapter = new FastListAdapter(R.layout.item_list_fastnews_old, null, mContext);
        return mListAdapter;
    }

    @Override
    public String getUrl() {
        if ("keyword".equals(mKey)){
            return NetConstant.SEARCH_NEWS_LIST;
        }
        return NetConstant.BLOCK_NEWS;
    }

    @Override
    public HashMap<String, String> initHashMap() {
        HashMap<String, String> map = new HashMap<>();
        map.put(getArguments().getString(Constant.ZERO), getArguments().getString(Constant.ONE));
        map.put("post_date", getPageNo("0"));
        map.put("isloadmore", String.valueOf(isloadmore));
        map.put("lastpost",mLastPost);
        if ("keyword".equals(mKey)){
            map.put("tab","flash");
        }
        mListAdapter.setSearchKeyWord(getArguments().getString(Constant.ONE));
        return map;
    }

    @Override
    public FastListPOJO parseData(String resp) {
        if ("keyword".equals(mKey)){
            BaseCodeResponse<FastListPOJO> pojo = GsonUtil.buildGson().fromJson(resp,new TypeToken<BaseCodeResponse<FastListPOJO>>(){}.getType());
            return pojo.data;
        }
        return GsonUtil.buildGson().fromJson(resp, FastListPOJO.class);
    }

    @Override
    protected void adapterSetData() {
        mListAdapter.setOnSharedClickListener(this);
        mAdapter.setNewData(mPOJO.getList());
    }

    @Override
    protected void adapterAddData() {
        if (mPOJO.getList() == null) {
            list_swipe.finishLoadmore();
            return;
        }
        mAdapter.addData(mPOJO.getList());
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        if (mPOJO != null && mPOJO.getList() != null&&mPOJO.getList().size()!=0) {
            mLastPost = mPOJO.getList().get(mPOJO.getList().size() - 1).getPost_date();
            int lastInt = FormatUtil.parseInt(mLastPost) - 24 * 60 * 60;
            setPageNo(FormatUtil.getTimetoHMS(lastInt, "yyyyMMdd"));

            isloadmore = true;
            initNet(true);
        } else {
            list_swipe.finishLoadmore();
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        setPageNo("");
        mLastPost = "";
        isloadmore = false;
        initNet(false);
    }

    @Override
    public void onShareClicked(FastListPOJO.ListBean item) {
        BlockMessageShareData shareData = new BlockMessageShareData();
        shareData.mTitle = item.getTitle();
        shareData.mContent = item.getContent();
        shareData.mImage = item.getImage();
        shareData.mPostTime = item.getPost_date();
        shareData.mVerifyName = item.getAuthor_info().getDisplay_name();
        shareData.mSourceImage = item.getSource().getIco();
        shareData.mSourceName = item.getSource().getName();
        String url = "https://www.8btc.com/article/"+item.getId();
        new ShareRouter.Builder()
                .context(mContext)
                .shareType(ShareType.BLOCk_MESSAGE)
                .url(url)
                .thumb(item.getImage())
                .describe(item.getDesc())
                .title(item.getTitle())
                .blockMessageShareData(shareData)
                .umShareListener(umShareListener)
                .build()
                .show();
    }

    UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {

        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {

        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {

        }
    };
}
